package com.example.simple.work.service.impl;

import com.example.simple.work.entity.Facility;
import com.example.simple.work.mapper.FacilityMapper;
import com.example.simple.work.service.IFacilityService;
import com.example.simple.work.service.ex.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class FacilityServiceImpl implements IFacilityService {
    @Autowired
    private FacilityMapper facilityMapper;

    /** 添加设备登记信息 */
    @Override
    public void insert(Facility facility) {
        // 登记设备信息时要求员工工号不能为空
        String employeeId = facility.getEmployeeId();
        if(employeeId == null || employeeId.equals("")){
            throw new UserIdNotExistException("员工工号不能为空");
        }

        // 补全数据
        facility.setIsDelete(0);
        // 补全数据
        facility.setCreateUser("崔阳亮");
        facility.setModifiedUser("崔阳亮");
        Date date = new Date();
        facility.setCreateTime(date);
        facility.setModifiedTime(date);

        Integer rows = facilityMapper.insert(facility);
        if(rows != 1){
            throw new InsertException("设备登记信息输入时存在未知错误");
        }
    }

    /** 根据id删除设备信息 */
    @Override
    public void delete(Integer id) {
        Facility result = facilityMapper.findFacilityById(id);
        if(result == null){
            throw new FacilityIdNotExistException("设备信息id不存在");
        }
        Integer rows = facilityMapper.deleteById(id);
        if(rows != 1){
            throw new FacilityException("删除设备登记信息出现未知异常");
        }
    }

    @Override
    public List<Facility> findAllFacility() {
        List<Facility> list = facilityMapper.findAllFacility();
        return list;
    }

    @Override
    public Facility findById(Integer id) {
        Facility result = facilityMapper.findFacilityById(id);
        if(result == null){
            throw new FacilityByIdNotExistException("数据不存在");
        }
        return result;
    }


}
